package com.jinbo.footmessage.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Selection;
import android.text.TextUtils;
import android.view.View;

import com.jinbo.footmessage.BaseActivity;
import com.jinbo.footmessage.MyApplication;
import com.jinbo.footmessage.R;
import com.jinbo.footmessage.utils.LocalPreferencesHelper;
import com.jinbo.footmessage.utils.VerifyUtils;
import com.jinbo.footmessage.view.CrossEditText;
import com.jinbo.footmessage.view.TitleBarView;


/**
 *
 */
public class PersonInfoUpdateTextActivity extends BaseActivity implements View.OnClickListener {
    private TitleBarView tbv_titleBar;
    private CrossEditText edt_text;
    private String text;
    private int  code;
// code 0:修改用户名 1：修改邮箱
    public static void launch(Activity activity, int requestCode, String text,int code) {
        Intent intent = new Intent(activity, PersonInfoUpdateTextActivity.class);
        intent.putExtra("text", text);
        intent.putExtra("code",code);
        activity.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent=getIntent();
        code=intent.getIntExtra("code",-1);
        text=intent.getStringExtra("text");
        setContentView(R.layout.activity_personinfo_update_text);
        initTitleBarView();
        initView();
    }

    private void initView() {
        String id=MyApplication.local.getString(LocalPreferencesHelper.USER_ID);
        edt_text= (CrossEditText) findViewById(R.id.edt_text);
        if ((code==0&&MyApplication.local.getString(id+LocalPreferencesHelper.USER_NAME).equals("")))
        {
            edt_text.setHint("请输入用户名");

        }
        else if ((code==1&&MyApplication.local.getString(id+LocalPreferencesHelper.USER_MAIl).equals("")))
        {
            edt_text.setHint("请绑定邮箱");

        }
        else
        {   edt_text.setText(text);
            int length = edt_text.getText().length();
            Selection.setSelection(edt_text.getText(), length);
        }

    }

    private void initTitleBarView() {
        tbv_titleBar = (TitleBarView) findViewById(R.id.tbv_titleBar);
        tbv_titleBar.setLeftBackButtonIsVisibler(true);
        if(code==0)
           tbv_titleBar.setCenterTitle("用户名");
        if(code==1)
            tbv_titleBar.setCenterTitle("邮箱");
        tbv_titleBar.setRightTextIsVisible(true, "保存");
        tbv_titleBar.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);
        tbv_titleBar.setListener(TitleBarView.TitleBarEnum.ll_rightNextTextLayout, this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_back_layout:
                finish();
                break;
            case R.id.right_next_text_layout:
                check();
                break;
        }
    }

    private void check() {
        text = edt_text.getText().toString();
        if (TextUtils.isEmpty(text)) {
           /* DialogUtils dialogUtils= new DialogUtils(PersonInfoUpdateTextActivity.this, 0,"center", true)
                    .setButtons(null, "确定", null);*/
            if(code==0)
               // dialogUtils.setMessage("请输入用户名");
                show("请输入用户名");
            if(code==1)
                show("请输入邮箱名");
               // dialogUtils .setMessage("请输入邮箱名");
          //  dialogUtils.create().show();
            return;
        }
        if(code==1&&!VerifyUtils.isMail(text))
        {
          /*      new DialogUtils(PersonInfoUpdateTextActivity.this,0, "center", true)
                        .setMessage("邮箱格式不对")
                        .setButtons(null, "确定", null).create().show();*/
                show("邮箱格式不对");
                return;
        }
        if(code==0)
        {
           //MyApplication.local.saveOrUpdate("user_name",text);
        }
        if(code==1)
        {
           //MyApplication.local.saveOrUpdate("user_mail",text);
        }
        Intent intent = new Intent();
        intent.putExtra("text", text);
        // 设置结果，并进行传送
        setResult(RESULT_OK, intent);
        finish();
    }
}
